package com.wanshi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanshi.entity.PasswordReq;
import com.wanshi.entity.Staff;
import com.wanshi.mapper.StaffMapper;
import com.wanshi.service.IStaffService;
import com.wanshi.utils.JwtUtil;
import com.wanshi.utils.MD5Utils;
import com.wanshi.utils.Result;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 员工信息表 服务实现类
 * </p>
 *
 * @author 顽石教育-www.wanshi.com
 * @since 2021-12-16
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements IStaffService {

    @Autowired
    private StaffMapper staffMapper;

    @Autowired
    private JwtUtil jwtUtil;


    @Override
    public Result add(Staff staff) {
//        if(staff==null || StringUtils.isEmpty(staff.getUsername())){
//            return Result.error("用户名不能为空");
//        }
        Staff s = staffMapper.findAllByName(staff.getUsername());
        if(s!=null){
            return Result.error("用户名已存在");
        }
        //String password = new BCryptPasswordEncoder().encode(staff.getPassword());
        String password = MD5Utils.md5(staff.getPassword());
        staff.setPassword(password);
        boolean b = this.save(staff);
        if(b){
            return Result.ok();
        }
        return Result.error("添加用户失败");
    }



    @Override
    public Staff findAllByName(String username) {
        return staffMapper.findAllByName(username);
    }

    @Override
    public Result checkPassowrd(PasswordReq req) {
        if(req == null || StringUtils.isEmpty(req.getPassword())){
                return Result.error("原密码不能为空");
        }
        Staff staff = baseMapper.selectById(req.getUserId());
       /* if(!new BCryptPasswordEncoder().matches(req.getPassword(),staff.getPassword())){
            return Result.error("原密码不正确");
        }*/
        String password = MD5Utils.md5(req.getPassword());
        if(!password.equals(staff.getPassword())){
            return Result.error("原密码不正确");
        }
        return Result.ok();
    }

    @Override
    public Result UpdatePassword(PasswordReq req) {
        if(req ==null ||StringUtils.isEmpty(req.getPassword())){
            return Result.error("新密码不能为空");
        }
        Staff staff = baseMapper.selectById(req.getUserId());
        String password = MD5Utils.md5(req.getPassword());
        staff.setPassword(password);
        baseMapper.updateById(staff);
        return Result.ok();
    }



    @Override
    public Result login(String username, String password) {
        Result error = Result.error("用户名或密码错误");
//        if(!StringUtils.isEmpty(username) || !StringUtils.isEmpty(password)){
//            System.out.println(123);
//            return  error;
//        }

        String password1 = MD5Utils.md5(password);
        Staff staff = staffMapper.findAllByNameAndPas(username, password1);
        if(staff==null){
              return error;
        }


        //System.out.println(password);
        //System.out.println(staff.getPassword());
//        if( !BCrypt.checkpw(password, staff.getPassword())) {
//            System.out.println(789);
//            return error;
//        }


        String jwt = jwtUtil.createJWT(staff.getId() + "", staff.getUsername(), true);
        Map<String,String> map = new HashMap<>();
        map.put("token", jwt);
        return Result.ok(map);
    }

    @Override
    public Result getUserInfo(String token) {
        Claims claims = jwtUtil.parseJWT(token);
        if(claims == null || StringUtils.isEmpty(claims.getSubject())) {
            return Result.error("获取用户信息失败");
        }
        String username = claims.getSubject();
        Staff staff = staffMapper.findAllByName(username);
        if(staff==null){
            return Result.error("用户不存在");
        }
        staff.setPassword(null);

        return Result.ok(staff);
    }
}
